﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 温湿度和颗粒度监控测试.Common;
using 温湿度和颗粒度监控测试.Common.Modbus;
using System.Windows.Input;
using 温湿度和颗粒度监控测试.Common.传感器;

namespace 温湿度和颗粒度监控测试.ViewModel.温湿度
{
    public class 温湿度测试WindowViewModel: NotifyBase
    {
        public 温湿度测试WindowViewModel()
        {
        }

        public delegate void MessageHandler(string msg);
        public MessageHandler ErrorMessageEvent;
        public MessageHandler InfoMessageEvent;


        private string _OutputText;
        /// <summary>
        /// 输出文本
        /// </summary>
        public string OutputText { get => _OutputText; set => SetProperty(ref _OutputText, value); }

        public ICommand 测试读取温度 => new CommandBase(param =>
        {
            try
            {
                if (温湿度传感器Client == null) throw new ApplicationException("未打开串口，请先打开串口");

                double 点1温度 = 温湿度传感器Client.获取温度值(1);
                double 点2温度 = 温湿度传感器Client.获取温度值(2);

                OutputText += $"[{DateTime.Now:HH:mm:ss}]温度，点1：{点1温度}℃，点2：{点2温度}℃\r\n";
            }
            catch (ApplicationException ex)
            {
                OutputText += $"[{DateTime.Now:HH:mm:ss}]{ex.Message}\r\n";
                //ErrorMessageEvent?.Invoke(ex.Message);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                ErrorMessageEvent?.Invoke(ex.Message);
            }
        });

        public ICommand 测试读取湿度 => new CommandBase(param =>
        {
            try
            {
                if (温湿度传感器Client == null) throw new ApplicationException("未打开串口，请先打开串口");

                double 点1湿度 = 温湿度传感器Client.获取湿度值(1);
                double 点2湿度 = 温湿度传感器Client.获取湿度值(2);

                OutputText += $"[{DateTime.Now:HH:mm:ss}]湿度，点1：{点1湿度}%，点2：{点2湿度}%\r\n";
            }
            catch (ApplicationException ex)
            {
                OutputText += $"[{DateTime.Now:HH:mm:ss}]{ex.Message}\r\n";
                //ErrorMessageEvent?.Invoke(ex.Message);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                ErrorMessageEvent?.Invoke(ex.Message);
            }
        });

        温湿度记颗粒度传感器 温湿度传感器Client;

        private bool _ModbusClientIsConnected = false;
        /// <summary>
        /// Modbus客户端是否连接状态
        /// </summary>
        public bool ModbusClientIsConnected { get => _ModbusClientIsConnected; set => SetProperty(ref _ModbusClientIsConnected, value); }

        public ICommand 打开串口 => new CommandBase(param =>
        {
            try
            {
                if (温湿度传感器Client == null) 温湿度传感器Client = new 温湿度记颗粒度传感器();

                if (ModbusClientIsConnected)
                {
                    // 断开串口
                    OutputText += $"[{DateTime.Now:HH:mm:ss}]关闭串口\r\n";

                    温湿度传感器Client.Close();
                    ModbusClientIsConnected = false;
                }
                else
                {
                    // 打开串口
                    OutputText += $"[{DateTime.Now:HH:mm:ss}]打开串口\r\n";

                    温湿度传感器Client.PortName = "COM4";
                    ModbusClientIsConnected = true;
                }
            }
            catch (ApplicationException ex)
            {
                ErrorMessageEvent?.Invoke(ex.Message);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                ErrorMessageEvent?.Invoke(ex.Message);
            }
        });
    }
}
